/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2012-2016 OpenFOAM Foundation
     \\/     M anipulation  |
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.

\*---------------------------------------------------------------------------*/

#include "fvCFD.H"

#include "volFields.H"
#include "fvcGrad.H"

// * * * * * * * * * * * * Protected Member Functions  * * * * * * * * * * * //

template<class Type>
void Foam::functionObjects::s_uuBudget::processField
(
    const word& fieldName,
    const tmp<GeometricField<Type, fvPatchField, volMesh>>& tvalue
)
{
    typedef GeometricField<Type, fvPatchField, volMesh> FieldType;

    const word scopedName = word("s_uu_") + fieldName;

    if (obr_.foundObject<FieldType>(scopedName))
    {
        FieldType& fld =
            const_cast<FieldType&>(obr_.lookupObject<FieldType>(scopedName));
        fld == tvalue();
    }
    else if (obr_.found(scopedName))
    {
        WarningInFunction
            << "Cannot store turbulence field " << scopedName
            << " since an object with that name already exists"
            << nl << endl;
    }
    else
    {
      obr_.store
        (
            new FieldType
            (
                IOobject
                (
                    scopedName,
                    obr_.time().timeName(),
                    obr_,
                    IOobject::READ_IF_PRESENT,
                    IOobject::NO_WRITE
                ),
                tvalue
            )
        );
    }
}
//2021.4.29

// returns the production term 产生项P_k
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::s_uuBudget::Pk() const
{
  const volVectorField& U = obr_.lookupObject<volVectorField>("U");
  const volVectorField& UMean = obr_.lookupObject<volVectorField>("UMean");
  const volSymmTensorField& UP2M = obr_.lookupObject<volSymmTensorField>("UPrime2Mean");
  const volScalarField& T_1 = obr_.lookupObject<volScalarField>("T_1"); 
  const volScalarField& T_1Mean = obr_.lookupObject<volScalarField>("T_1Mean"); 
  const volScalarField T_1Prime = T_1 - T_1Mean;
  const volVectorField UPrime = U - UMean;
  //先选择一个温度场T_1作为测试,后续得加上八个
  const volVectorField gradT_1( fvc::grad(T_1Mean) );
  const volVectorField U2M_grdT(UP2M & gradT_1);
  const volScalarField R_1(U2M_grdT.component(vector::X));
  /********result_1********/
  const volVectorField UPrime_TPrime(UPrime * T_1Prime);
  const volScalarField UMeanX(UMean.component(vector::X));
  const volVectorField UMGrad(fvc::grad(UMeanX));
  const volScalarField R_2(UPrime_TPrime & UMGrad);
  //出现新的需要时均的场的时候，在求解器把这一项read进去
  /********result_2********/
  const volScalarField R_P( R_1 + R_2 );
  

  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Pk",
	      R_P.mesh().time().timeName(),
	      R_P.mesh()
	   ),
	  (-1.0)*R_P,
	  R_P.boundaryField().types()
       )
   );
}
//OpenFOAM执行相加操作时会进行量纲检查
//把Dk拆成三项，不相加

// returns the viscous diffusion term 1 
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::s_uuBudget::Dk_1() const
{

  const volScalarField& T_1 = obr_.lookupObject<volScalarField>("T_1"); 
  const volScalarField& T_1Mean = obr_.lookupObject<volScalarField>("T_1Mean"); 
  const volScalarField T_1Prime = T_1 - T_1Mean;


  const volScalarField& p = obr_.lookupObject<volScalarField>("p"); 
  const volScalarField& pMean = obr_.lookupObject<volScalarField>("pMean"); 
  const volScalarField pPrime = p - pMean;
  
  const volVectorField pPrime_grd = fvc::grad(pPrime);
  const volVectorField pP_TP = fvc::grad(pPrime * T_1Prime);

  const volScalarField R_1 = (-1.0)*pP_TP.component(vector::X);  //Dk的第一项
 

//   const volScalarField R_D = R_1 + R_2 + R_3; 估计是这项进行相加的时候量纲不对
   

  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Dk_1",
	      R_1.mesh().time().timeName(),
	      R_1.mesh()
	   ),
	  R_1, 
	  R_1.boundaryField().types()
       )
   );
}

// returns the viscous diffusion term 2
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::s_uuBudget::Dk_2() const
{
  const volVectorField& U = obr_.lookupObject<volVectorField>("U");
  const volVectorField& UMean = obr_.lookupObject<volVectorField>("UMean");
  const volVectorField UPrime = U-UMean;
  const volScalarField UPrimeX(UPrime.component(vector::X)); 
  const volVectorField UPrimeX_grd(fvc::grad(UPrimeX));
  
  const volScalarField& T_1 = obr_.lookupObject<volScalarField>("T_1"); 
  const volScalarField& T_1Mean = obr_.lookupObject<volScalarField>("T_1Mean"); 
  const volScalarField T_1Prime = T_1 - T_1Mean;
  const volVectorField T_1P_grd = fvc::grad(T_1Prime);

  const volScalarField s_uuDk_2 = ((T_1P_grd & UPrimeX_grd) + (UPrimeX*fvc::laplacian(T_1Prime))); //Dk的第二项
  const volScalarField R_2 = ( nu_s_uu/Pr_1 ) * s_uuDk_2;
   
  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Dk_2",
	      R_2.mesh().time().timeName(),
	      R_2.mesh()
	   ),
	  R_2, 
	  R_2.boundaryField().types()
       )
   );
}


// returns the viscous diffusion term 3
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::s_uuBudget::Dk_3() const
{
  const volVectorField& U = obr_.lookupObject<volVectorField>("U");
  const volVectorField& UMean = obr_.lookupObject<volVectorField>("UMean");
  const volVectorField UPrime = U-UMean;
  const volScalarField UPrimeX(UPrime.component(vector::X)); 
  const volVectorField UPrimeX_grd(fvc::grad(UPrimeX));
  
  const volScalarField& T_1 = obr_.lookupObject<volScalarField>("T_1"); 
  const volScalarField& T_1Mean = obr_.lookupObject<volScalarField>("T_1Mean"); 
  const volScalarField T_1Prime = T_1 - T_1Mean;
  const volVectorField T_1P_grd = fvc::grad(T_1Prime);
  
 // const volScalarField& p = obr_.lookupObject<volScalarField>("p"); 
 // const volScalarField& pMean = obr_.lookupObject<volScalarField>("pMean"); 
 // const volScalarField pPrime = p - pMean;
 // const volVectorField pPrime_grd = fvc::grad(pPrime);

  const volVectorField s_uuDk_3( T_1Prime * UPrimeX_grd ); 
  const volScalarField R_3( nu_s_uu * fvc::div(s_uuDk_3));   //Dk的第三项
   
  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Dk_3",
	      R_3.mesh().time().timeName(),
	      R_3.mesh()
	   ),
	  R_3, 
	  R_3.boundaryField().types()
       )
   );
}

// returns the viscous diffusion term 4
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::s_uuBudget::Dk_4() const
{
  const volVectorField& U = obr_.lookupObject<volVectorField>("U");
  const volVectorField& UMean = obr_.lookupObject<volVectorField>("UMean");
  const volVectorField UPrime = U-UMean;
  const volScalarField UPrimeX = (UPrime.component(vector::X));
  
  const volScalarField& T_1 = obr_.lookupObject<volScalarField>("T_1"); 
  const volScalarField& T_1Mean = obr_.lookupObject<volScalarField>("T_1Mean"); 
  const volScalarField T_1Prime = T_1 - T_1Mean;

   //const volScalarField R_4 ( fvc::div(UPrimeX * UPrime * T_1Prime));   //Dk的第四项
   //注意这个UPrimeX * UPrime * T_1Prime ，在fvScheme里面居然还要调用到离散格式
   //2021.6.5 决定拆开写 试试
  const volVectorField test_1 = UPrimeX * UPrime ;
  const volVectorField test_2 = test_1 * T_1Prime; 
  const volScalarField  R_4 = fvc::div(test_2);
   
  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Dk_4",
	      R_4.mesh().time().timeName(),
	      R_4.mesh()
	   ),
	  R_4, 
	  R_4.boundaryField().types()
       )
   );
}

// returns the dissipation term 耗散项 Epsilon
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::s_uuBudget::Epik() const
{
  const volVectorField& U = obr_.lookupObject<volVectorField>("U");
  const volVectorField& UMean = obr_.lookupObject<volVectorField>("UMean");
  const volVectorField UPrime =U-UMean;
 // const volTensorField UPrime_grd = fvc::grad(UPrime);
  const volScalarField UPrimeX = UPrime.component(vector::X);
  const volVectorField  UPrimeX_grd = (fvc::grad(UPrimeX));
  //这个地方别用tensor
  
  const volScalarField& T_1 = obr_.lookupObject<volScalarField>("T_1"); 
  const volScalarField& T_1Mean = obr_.lookupObject<volScalarField>("T_1Mean"); 
  const volScalarField T_1Prime = T_1 - T_1Mean;
  const volVectorField T_1Prime_grd = (fvc::grad(T_1Prime));
  
  const volScalarField R_Epi =(nu_s_uu/Pr_1 + nu_s_uu)*(UPrimeX_grd & T_1Prime_grd);

  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Epik",
	      R_Epi.mesh().time().timeName(),
	      R_Epi.mesh()
	   ),
	  (-1.0)*R_Epi,
	  R_Epi.boundaryField().types()
       )
   );
}


// returns the temperature-pressure flucatuation gradient term
Foam::tmp<Foam::volScalarField>
Foam::functionObjects::s_uuBudget::Pik() const
{
  const volScalarField& p = obr_.lookupObject<volScalarField>("p");
  const volScalarField& pMean = obr_.lookupObject<volScalarField>("pMean");
  const volScalarField pPrime = p - pMean;

  const volScalarField& T_1 = obr_.lookupObject<volScalarField>("T_1"); 
  const volScalarField& T_1Mean = obr_.lookupObject<volScalarField>("T_1Mean"); 
  const volScalarField T_1Prime = T_1 - T_1Mean;
  const volVectorField T_1Prime_grd = (fvc::grad(T_1Prime));
  const volScalarField T_1PPrimeX = T_1Prime_grd.component(vector::X);
  const volScalarField R_Pi = (pPrime * T_1PPrimeX);
   
  return tmp<volScalarField>
  (
      new volScalarField
      (
          IOobject
	  (
	      "Pik",
	      R_Pi.mesh().time().timeName(),
	      R_Pi.mesh()
	   ),
	  R_Pi,
	  R_Pi.boundaryField().types()
       )
   );
}


// ************************************************************************* //
